<?php
if ( !defined('IN_ADMINCP') || !defined('IN_SEC') ){
	exit('Access Denied.');
}
if( is_post() ){
	$content = $_G['gp_content'] ? htmlspecialchars($_G['gp_content']) : $admincp->cpmsg('请输入内容');
	$mobiles = $_G['gp_mobiles'];
	$mobiles = is_string($_G['gp_mobiles']) ? explode(',', str_replace("\n", ',', $mobiles)) : $mobiles;

	$sends = array();
	
	foreach( $mobiles as $k => $v ){
		$v = strval(trim($v));
		if ( is_mobile($v) ){
			$sends[$v] = $v;
		}
	}
	if( $sends && $content ){
		$success = send_sms($content, $sends);
		$admincp->cpmsg('发送成功！发送：' . $success . '条');
	}else{
		$admincp->cpmsg('手机号码错误！发送失败！');
	}
} 

function send_sms($content = '', $sends = array()){
	global $admincp;
	if( is_array($sends) ){
		$sent = count($sends);
		$sends = join(',', $sends);
	}
	$result = winic_send($sends, $content);
	$_result = explode('/', $result);
	$success = str_replace('Send:', '', $_result[1]);
	DB::Insert('sms_sent', array(
		'adminid' => $admincp->adminid,
		'sent' => $sent,
		'success' => $success,
		'mobiles' => $sends,
		'result' => $result,
		'service' => 'winic',
		'create_time' => SYS_TIME,
	));
	return $success;
}
function winic_send($strMobile = '', $content = ''){
	$url = "http://service.winic.org:8009/sys_port/gateway/";
	$id = urlencode('heivr.sms.new.oa');
	$pwd = urlencode('admin123');
	$to = urlencode($strMobile);
	$content = iconv('utf-8', 'gb2312', $content); //将utf-8转为gb2312再发

	$curlPost = 'id=%s&pwd=%s&to=%s&content=%s&time=';
	$curlPost = sprintf($curlPost, $id, $pwd, $to, $content);
	
	$ch = curl_init();//初始化curl
	curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
	curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
	curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
	curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
	$data = curl_exec($ch);//运行curl
	curl_close($ch);
	return $data;
} 

?>
